import {tradeCreate, zhimaCreate} from "@/api/order";
import {leasePay} from "@/api/leaseOrder";
const goodsDetailPlugin = requirePlugin('goodsDetailPlugin');
export default {
    methods: {
        zhimaCreatePre(oid) {
            let that = this

            // 创建交易组件
            my.checkBeforeAddOrder({
                success({requireOrder, sceneId, sourceId}) {
                    tradeCreate({
                        oid: oid,
                        source_id: sourceId,
                        path: 'pages/goods/lease_order_details/index'
                    }).then(res => {
                        if (res.status === 200) {
                            // 唤起去免押
                            that.zhimaCreate(oid)
                        }
                    })
                },
                fail({error, errorMessage}) {
                    that.$util.Tips({title: '订单前置判断失败'})
                },
                complete(e) {
                    // console.log(e)
                },
            })
        },
        // 创建芝麻免押订单
        zhimaCreate(oid){
             zhimaCreate({
                oid:oid
            }).then( async res=>{
                uni.hideLoading()
                if(res.status===200){
                    // 新-芝麻免押
                    const params = {
                        signStr: res.data?.sign_str, // 授权签名字符串
                        signLaunchMethod: res.data?.sign_launch_method, // 签约方式
                    }
                    const {success , resultCode} = await goodsDetailPlugin.triggerSignLaunchMethod(params) || {};

                    // 触发各页面后续逻辑
                    this.zhimaResult({success:success,resultCode:resultCode})

                }
            }).catch(err => {
                this.$util.Tips({
                    title: err.msg,
                    icon: 'error'
                });
                uni.hideLoading()
            })
        },


        // 支付宝支付
        payAlipay(oid,num) {
            uni.showLoading({
                title: '加载中...'
            })
            let that = this
            leasePay(oid, {
                num: num,
                paytype: 'alipay',
                from: 'ali_routine',
                quitUrl: 'pages/goods/lease_order_details/index'
            }).then(res => {
                let status = res.data.status,
                    orderId = res.data.result.order_id || '',
                    tradeNo = res.data.result.trade_no || '',
                    jsConfig = res.data.result.jsConfig;
                switch (status) {
                    case 'ALIPAY_PAY':
                        // 订单支付
                        if(tradeNo){
                            my.tradePay({
                                tradeNO: tradeNo,
                                success: (e) => {
                                    if (e.resultCode == 9000) {
                                        that.$util.Tips({title: '支付成功'})
                                    }
                                },
                                fail: (e) => {
                                    console.log('支付失败', e)
                                    that.$util.Tips({title: '支付失败'})
                                },
                                complete: (e) => {
                                    that.payResult()
                                },
                            });
                        } else {
                            // 老订单支付：兼容旧订单
                            uni.requestPayment({
                                provider: 'alipay',
                                orderInfo: jsConfig.tradeNo,
                                success: (e) => {
                                    console.log(e)
                                    if (e.resultCode == "6001") {
                                    } else {
                                        that.$util.Tips({title: '支付成功'})
                                    }
                                },
                                fail: (e) => {
                                    console.log('ALIPAY_PAY_fail', e);
                                    that.$util.Tips({title: '支付失败'})
                                },
                                complete: () => {
                                    that.payResult()
                                },
                            });
                        }
                        break;
                }
            }).finally(() => {
                uni.hideLoading()
            })
        },
    },
}